﻿  using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;
    using System.ComponentModel;
using DirectUI.Mobile.UI;
namespace DirectUI.Mobile.UI.Controls
{
  

        public class AnimateProgressBar :DirectUIControl
        {
            public Bitmap BackGroundImage
            {
                get;
                set;
            }
            public Bitmap ForeImage
            {
                get;
                set;
            }
            public float Max { get; set; }
            public float Min { get; set; }
            private float value;
            public float Value
            {
                get
                {
                    return value;
                }
                set
                {
                    if (value >= Min && value <= Max)
                    {
                        this.value = value;
                        this.Refresh();
                    }
                }
            }
            public AnimateProgressBar()
                : base()
            {
                BackGroundImage = Resource.progessBar_back;
                ForeImage = Resource.progressBar_ForegroundPicture;
                this.Width = Resource.progessBar_back.Width;
                this.Height = Resource.progessBar_back.Height;
                this.Min = 0;
                this.Max = 188;
            }
            private Bitmap dbBuffer;
            private void EnsureDBBuffer()
            {
                if (dbBuffer == null)
                {
                    dbBuffer = new Bitmap(this.Width, this.Height);
                }
            }
            public override void OnPaint(DirectUIPaintEventArgs e)
            {
                EnsureDBBuffer();
                using (Graphics g = Graphics.FromImage(dbBuffer))
                {
                //    ImageAttributes ia = new ImageAttributes();
                    //  ia.SetColorKey(Color.Fuchsia, Color.Fuchsia);
                 //   ia.SetColorKey(BackGroundImage.GetPixel(0, 0), BackGroundImage.GetPixel(0, 0));
            //  g.DrawImage(BackGroundImage, new Rectangle(0,0,this.Width,this.Height), 0, 0, this.Width, this.Height, GraphicsUnit.Pixel, ia);
                 //  g.DrawImage(BackGroundImage, 0, 0);
                    Rectangle desRec = new Rectangle(0, 0, this.Width, this.Height);
                    g.DrawImage(BackGroundImage, desRec, new Rectangle(0, 0, this.BackGroundImage.Width, this.BackGroundImage.Height), GraphicsUnit.Pixel);
                   int left = (int)(((float)Value / (float)(Max - Min)) * this.Width - this.Width);
                   // ia.SetColorKey(ForeImage.GetPixel(0, 0), ForeImage.GetPixel(0, 0));
        //int left = (int)(this.Width-((float)Value / (float)(Max - Min)) * this.Width  );

                  //g.DrawImage(ForeImage,  left, 0);
        g.DrawImage(ForeImage, new Rectangle(left, 0,this.Width, this.Height),new Rectangle(0,0,ForeImage.Width,ForeImage.Height), GraphicsUnit.Pixel);

                  
                }
               // e.Graphics.DrawImage(dbBuffer,e.ControlBounds.X,e.ControlBounds.Y);
                e.Graphics.DrawImage(dbBuffer, e.ControlBounds, new Rectangle(0, 0, dbBuffer.Width, dbBuffer.Height), GraphicsUnit.Pixel);
             //   e.Graphics.DrawImage(ForeImage, e.ControlBounds, left, 0, this.Width, this.Height, GraphicsUnit.Pixel, null);
              //  e.Graphics.DrawImage(ForeImage, e.ControlBounds, new Rectangle(left, 0, this.Width - left, this.Height), GraphicsUnit.Pixel);
            }
        }
    }

  
